<?php

namespace App\Admin\Metrics\Users;

use App\Models\WxAuthentication;
use App\Models\WxUser;
use Dcat\Admin\Widgets\Metrics\Line;
use Illuminate\Http\Request;

class AuthUsers extends Line
{
    /**
     * 初始化卡片内容
     *
     * @return void
     */
    protected function init()
    {
        parent::init();

        $this->title('认证');
        $this->dropdown([
            '7' => '全部',
//            '28' => 'Last 28 Days',
//            '30' => 'Last Month',
//            '365' => 'Last Year',
        ]);
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        $generator = function ($len, $min = 10, $max = 300) {
            for ($i = 0; $i <= $len; $i++) {
                yield mt_rand($min, $max);
            }
        };

        switch ($request->get('option')) {
//            case '365':
//                // 卡片内容
//                $this->withContent(mt_rand(1000, 5000).'k');
//                // 图表数据
//                $this->withChart(collect($generator(30))->toArray());
//                break;
//            case '30':
//                // 卡片内容
//                $this->withContent(mt_rand(400, 1000).'k');
//                // 图表数据
//                $this->withChart(collect($generator(30))->toArray());
//                break;
//            case '28':
//                // 卡片内容
//                $this->withContent(mt_rand(400, 1000).'k');
//                // 图表数据
//                $this->withChart(collect($generator(28))->toArray());
//                break;
//            case '7':
            default:
                $authCount = WxUser::where('is_authentication',1)->count();

                // 卡片内容
                $this->withContent($authCount);

                $oneUser = WxAuthentication::whereBetween('created_at',[date('Y-m-d H:i:s',strtotime('-1 day')),date('Y-m-d H:i:s')])->count();
                $twoUser = WxAuthentication::whereBetween('created_at',[date('Y-m-d H:i:s',strtotime('-2 day')),date('Y-m-d H:i:s')])->count();
                $threeUser = WxAuthentication::whereBetween('created_at',[date('Y-m-d H:i:s',strtotime('-3 day')),date('Y-m-d H:i:s')])->count();
                $fourUser = WxAuthentication::whereBetween('created_at',[date('Y-m-d H:i:s',strtotime('-4 day')),date('Y-m-d H:i:s')])->count();
                $fiveUser = WxAuthentication::whereBetween('created_at',[date('Y-m-d H:i:s',strtotime('-5 day')),date('Y-m-d H:i:s')])->count();
                $sixUser = WxAuthentication::whereBetween('created_at',[date('Y-m-d H:i:s',strtotime('-6 day')),date('Y-m-d H:i:s')])->count();
                $sevenUser = WxAuthentication::whereBetween('created_at',[date('Y-m-d H:i:s',strtotime('-7 day')),date('Y-m-d H:i:s')])->count();

                // 图表数据
                $this->withChart([$oneUser, $twoUser, $threeUser, $fourUser, $fiveUser, $sixUser, $sevenUser]);
        }
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'series' => [
                [
                    'name' => $this->title,
                    'data' => $data,
                ],
            ],
        ]);
    }

    /**
     * 设置卡片内容.
     *
     * @param string $content
     *
     * @return $this
     */
    public function withContent($content)
    {
        return $this->content(
            <<<HTML
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
    <h2 class="ml-1 font-lg-1">{$content}</h2>
    <span class="mb-0 mr-1 text-80">{$this->title}</span>
</div>
HTML
        );
    }
}
